/*
 * 文件名： JiuFragment.java
 * 描    述：MainActivity -- 九块邮
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.miguo.activity.base.BaseListFragment;
import com.miguo.activity.callback.IGoods;
import com.miguo.activity.callback.IGoodsScroll;
import com.miguo.activity.callback.listener.GoodsScrollListener;
import com.miguo.adapter.GoodsAdapter;
import com.miguo.bean.CommentBean;
import com.miguo.bean.GoodsBean;
import com.miguo.framework.component.BaseStatusView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.ui.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JiuListFragment extends BaseListFragment<GoodsBean, GoodsAdapter> implements View.OnClickListener, IGoodsScroll, IGoods<GoodsBean, GoodsAdapter> {

    private String tyep;
    private RelativeLayout mContent;
    private BaseStatusView BaseStatus;

    public static JiuListFragment newInstance(String tyep) {
        JiuListFragment fragment = new JiuListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tyep", tyep);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setGoodsCallBack(this);
        initExtra();
        findViewById();
        initView();
    }

    private void initExtra() {
        Bundle bundle = getArguments();
        tyep = bundle.getString("tyep");
    }

    @Override
    protected void findViewById() {
        super.findViewById();
        mContent = (RelativeLayout) view.findViewById(R.id.content_view);
        BaseStatus = (BaseStatusView) view.findViewById(R.id.base_status_view);
        BaseStatus.setContentView(mContent);
        BaseStatus.getRefreshView().setOnClickListener(this);
        BaseStatus.getNetworkView().setOnClickListener(this);
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.refresh_rl:
                this.onGoodsLoadData();
                break;
            case R.id.network_error_ll:
                this.onGoodsLoadData();
                break;
        }
        super.onClick(v);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_jiu_list, container, false);
        return view;
    }

    @Override
    public GoodsAdapter onGoodsLoadAdapter() {
        return new GoodsAdapter(mActivity);
    }

    @Override
    public List<GoodsBean> onGoodsDealList(JsonData data) {
        return CommentBean.GoodsData(data.optJson("item"));
    }

    @Override
    public void onGoodsLoadData() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("page", String.valueOf(page));
        param.put("cid", tyep);
        HttpClient.post(Url.toRoute(Url.Jiu.List, 1), param,
                AjaxCallBack);
    }

    @Override
    public GoodsScrollListener onGoodsScroll() {
        return new GoodsScrollListener(this);
    }

    @Override
    public void onGoodsRefresh() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFinish() {
        BaseStatus.setRefreshVisibility(View.GONE);
    }

    @Override
    public void onGoodsError() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFail() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onScrollIdle() {
        imageLoader.resume();
    }

    @Override
    public void onScrollFling() {
        if (pauseOnFling) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTouchScroll() {
        if (pauseOnScroll) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTopPositiont() {
        // 判断滚动到顶部
        if (mToTopBtn.getVisibility() == View.VISIBLE) {
            mToTopBtn.setVisibility(View.GONE);
            mToTopBtn.startAnimation(mTopDownAnima);
        }
    }

    @Override
    public void onScrollFirstLastPosition() {
        // 第一次滚动至底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
    }

    @Override
    public void onScrollLastPosition() {
        // 判断滚动到底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
        if (pageSize >= page + 1) {
            page++;
            isRefresh = false;
            this.onGoodsLoadData();
        } else {
            Toast.show(mContext, "已经到底了!");
            pullToRefresh.onRefreshComplete();
        }
    }
}
